<?php

    function __autoload($class_name) 
    {
        include $class_name . '.php';
    }
    $page = new page("login");
    $content="";
    if(isset($_SESSION['user']))
    {
        try
        {
            $_SESSION['user']->validate();
        }catch(Exception $e)
        {
            header('location:login.php');
        }
        $content .= "Jste prihlasen jako ". $_SESSION['user']->__get('name') . "<br />";
    }else
    {
        //TODO: stranka pro hosta
        //login form
        if(isset($_POST['login']))
        {
            //prihlasit uzivatele
            $user = new user();
            //kontrola hodnot
            try
            {
                checker::checkString($_POST['login']);
                checker::checkString($_POST['password']);
                $user->login($_POST['login'],$_POST['password']);
                header('Location: index.php');  
            }catch(Exception $e)
            {
                $content .= "Chybne prihlasovaci udaje!";
                $content .= $_POST['login']."<br />";
            }            
        }  
        
            //zobrazit formular pro prihlaseni
            $login_form = new form("loginForm","login.php", "post", "Login:",
                    array(
                        new formText("login",'Login: ', '', 20),
                        new formPassword("password",'Heslo: ', '',32),
                        new formSubmit("Prihlasit")));
            $content .= $login_form;
            $content .= '<br /><a href="registration.php">registrace</a>';
        
    }
    echo $page($content);
?>
